package org.bookshare.document.reader;

import java.io.File;
import java.io.IOException;

import org.benetech.event.EventListener;
import org.benetech.util.ViolationCollatingErrorHandler;
import org.benetech.util.ZipContents;
import org.benetech.util.ZipUtils;
import org.bookshare.document.beans.DocumentType;
import org.bookshare.document.beans.OEBDocumentSet;
import org.bookshare.document.beans.DocumentType.OEB;

/**
 * Reads an OEB Zip.
 * @author Reuben Firmin
 */
public final class OEBReader extends AbstractReader<DocumentType.OEB> implements Reader {

	/**
	 * Default constructor.
	 */
	public OEBReader() {
		// empty
	}

    /**
     * {@inheritDoc}
     */
	public OEB getDocumentType() {
	    return (OEB) DocumentType.OEB;
    }

    /**
     * {@inheritDoc}
     */
    public OEBDocumentSet read(final File file, final EventListener listener) throws IOException {
        if (file == null || !file.exists()) {
            throw new IOException("File cannot be located");
        }

        if (getTmpDir() == null) {
            throw new IOException("Tmp dir is not initialized");
        }

        listener.message("Phase (1) Unpack zip to temporary directory");
        // extract the zip to a temporary directory
        final ZipContents zipContents = ZipUtils.extractZip(file, getTmpDir());
        final ViolationCollatingErrorHandler handler = new ViolationCollatingErrorHandler();
        return new OEBDocumentSet(zipContents, zipContents.getBasePath(), listener, handler);
    }
}
